import React from 'react';
import {Spin, Card, Button, Row, Col, Form, Checkbox} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {FORM_OPTIONS, DATE_FORMAT} from '../constants';
import {PERMISSION} from './constants';
import {hasPermissions} from './utils';
import {getDetail} from './api';
import {injectIntl} from './intl';
import {formatDateTime} from '../utils';
import styles from './style.css';
import Return from '../common/Return';
const FormItem = Form.Item;
const checkboxColLeft = {
    xxl: {span: 6},
    xl: {span: 8},
    md: {span: 8}
};
const checkboxColRight = {
    xxl: {span: 18},
    xl: {span: 16},
    md: {span: 16}
};
class NewPanel extends React.PureComponent {
    state = {
        loading: false,
        hasOperat: false,
        data: {},
        showPartMultiSelectPanel: false,
        showDealerSelectPanel: false,
        details: []
    }

    componentDidMount() {
        if(this.props.id) {
            this.setState({loading: true});
            getDetail(this.props.id)
                .then(res => {
                    if(res.ok)
                        this.setState({
                            loading: false,
                            data: res.data,
                            details: res.data.details
                        });
                    else
                        this.setState(({
                            loading: false
                        }));
                });
        }
    }
    componentWillUnmount() {
        this.isunmount = true;
    }
    isWorkingDayCheck = e => {
        const isWorkingDay = e.target.checked;
        const data = Object.assign({}, this.state.data, {
            isWorkingDay
        });
        this.setState({
            data
        });
    }
    onClickOkOnPopconfirm = () => {
        this.props.onCancel();
    }
    onSubmit = () => {
        const {data} = this.state;
        this.setState({loading: true});
        this.props.onSubmit(data).then(res => {
            if(res.success)
                this.props.successReturn();
            if(!this.isunmount)
                this.setState({loading: false});
        });
    }
    render() {
        const {data} = this.state;
        const {formatMessage} = this.props.intl;
        const submitable = !hasPermissions(PERMISSION.update, this.props.permissions);
        return (
            <div className="form-standard">
                <Spin spinning={this.state.loading}>
                    <Card title={formatMessage({
                        id: 'newPanel.column.common',
                        defaultMessage: '工作日历'
                    })}>
                        <Form>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.operation.brandName',
                                        defaultMessage: '品牌'
                                    })} {...FORM_OPTIONS.item}>
                                        {data.brandName}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.operation.day',
                                        defaultMessage: '日期'
                                    })} {...FORM_OPTIONS.item}>
                                        {formatDateTime(data.day, DATE_FORMAT)}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <Row className={styles.rowCheckbox}>
                                        <Col {...checkboxColLeft} />
                                        <Col {...checkboxColRight}>
                                            <Checkbox
                                                checked={data.isWorkingDay}
                                                name="isWorkingDay"
                                                value={data.isWorkingDay}
                                                onChange={this.isWorkingDayCheck}>
                                                {this.props.intl.formatMessage({
                                                    id: 'newPanel.form.isWorkingDay',
                                                    defaultMessage: '是否工作日'
                                                })}
                                            </Checkbox>
                                        </Col>
                                    </Row>
                                </Col>
                            </Row>
                        </Form>
                    </Card>
                    <Card className={styles.optionBtn}>
                        <Button
                            type="primary"
                            onClick={this.onSubmit}
                            disabled={!submitable}
                            loading={this.state.loading}>
                            {formatMessage({
                                id: 'newPanel.operation.onSave',
                                defaultMessage: '保存'
                            })}
                        </Button>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Return
                        disabled={this.state.loading}
                        onConfirm={this.onClickOkOnPopconfirm}
                        showWarning={this.state.hasOperat} />
                </div>
            </div>
        );
    }
}

NewPanel.propTypes = {
    brands: PropTypes.array.isRequired,
    permissions: PropTypes.array.isRequired,
    successReturn: PropTypes.func.isRequired,
    onCancel: PropTypes.func.isRequired,
    onSubmit: PropTypes.func.isRequired,
    enterprise: PropTypes.object,
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
};

import {connect} from 'react-redux';
import {
    tableSearch,
    onSubmit
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';
const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'initData']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.id
        })) : [];
        return brands;
    }
);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = (state, props) => ({
    brands: getinitEnumData(state),
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    onSubmit: data => dispatch(onSubmit(data)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(NewPanel));
